/*
 * @文件描述: 按键模块头文件，引脚定义
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-01-20 13:30:17
 */
#ifndef MYBUTTON_H
#define MYBUTTON_H

#include "public.h"
#include <rtthread.h>
//#include "stm32f4xx.h"
#define KEY_ON 1
#define KEY_OFF 0

//顺时针方向，
#define ENCODER_CW 1
//逆时针方向
#define ENCODER_CCW 2
#ifndef WIN_SIM

//key1
#define KEY1_GPIO_PIN GPIO_Pin_4
#define KEY1_GPIO_PORT GPIOE
#define KEY1_GPIO_CLK RCC_AHB1Periph_GPIOE
//key2
#define KEY2_GPIO_PIN GPIO_Pin_0
#define KEY2_GPIO_PORT GPIOA
#define KEY2_GPIO_CLK RCC_AHB1Periph_GPIOA

//button board f1
#define F1_GPIO_PIN GPIO_Pin_13
#define F1_GPIO_PORT GPIOG
#define F1_GPIO_CLK RCC_AHB1Periph_GPIOG
//button board f2
#define F2_GPIO_PIN GPIO_Pin_9
#define F2_GPIO_PORT GPIOG
#define F2_GPIO_CLK RCC_AHB1Periph_GPIOG
//button board f3
#define F3_GPIO_PIN GPIO_Pin_11
#define F3_GPIO_PORT GPIOG
#define F3_GPIO_CLK RCC_AHB1Periph_GPIOG
//button board f4
#define F4_GPIO_PIN GPIO_Pin_14
#define F4_GPIO_PORT GPIOG
#define F4_GPIO_CLK RCC_AHB1Periph_GPIOG
//button board f5
#define F5_GPIO_PIN GPIO_Pin_15
#define F5_GPIO_PORT GPIOG
#define F5_GPIO_CLK RCC_AHB1Periph_GPIOG

//ENCODER1 CHECKED
#define ENCODER1_A_GPIO_PIN GPIO_Pin_1
#define ENCODER1_A_GPIO_PORT GPIOA
#define ENCODER1_A_GPIO_CLK RCC_AHB1Periph_GPIOA

#define ENCODER1_A_EXIT_PORT EXTI_PortSourceGPIOA
#define ENCODER1_A_EXIT_PIN EXTI_PinSource1
#define ENCODER1_A_EXIT_LINE EXTI_Line1

#define ENCODER1_B_GPIO_PIN GPIO_Pin_2
#define ENCODER1_B_GPIO_PORT GPIOA
#define ENCODER1_B_GPIO_CLK RCC_AHB1Periph_GPIOA

#define ENCODER1_BUT_GPIO_PIN GPIO_Pin_3
#define ENCODER1_BUT_GPIO_PORT GPIOA
#define ENCODER1_BUT_GPIO_CLK RCC_AHB1Periph_GPIOA

//ENCODER2 CHECKED
#define ENCODER2_A_GPIO_PIN GPIO_Pin_0
#define ENCODER2_A_GPIO_PORT GPIOB
#define ENCODER2_A_GPIO_CLK RCC_AHB1Periph_GPIOB

#define ENCODER2_A_EXIT_PORT EXTI_PortSourceGPIOB
#define ENCODER2_A_EXIT_PIN EXTI_PinSource0
#define ENCODER2_A_EXIT_LINE EXTI_Line0

#define ENCODER2_B_GPIO_PIN GPIO_Pin_1
#define ENCODER2_B_GPIO_PORT GPIOB
#define ENCODER2_B_GPIO_CLK RCC_AHB1Periph_GPIOB

#define ENCODER2_BUT_GPIO_PIN GPIO_Pin_6
#define ENCODER2_BUT_GPIO_PORT GPIOB
#define ENCODER2_BUT_GPIO_CLK RCC_AHB1Periph_GPIOB

//ENCODER3
#define ENCODER3_A_GPIO_PIN GPIO_Pin_3
#define ENCODER3_A_GPIO_PORT GPIOD
#define ENCODER3_A_GPIO_CLK RCC_AHB1Periph_GPIOD

#define ENCODER3_A_EXIT_PORT EXTI_PortSourceGPIOD
#define ENCODER3_A_EXIT_PIN EXTI_PinSource3
#define ENCODER3_A_EXIT_LINE EXTI_Line3

#define ENCODER3_B_GPIO_PIN GPIO_Pin_6
#define ENCODER3_B_GPIO_PORT GPIOD
#define ENCODER3_B_GPIO_CLK RCC_AHB1Periph_GPIOD

#define ENCODER3_BUT_GPIO_PIN GPIO_Pin_7
#define ENCODER3_BUT_GPIO_PORT GPIOB
#define ENCODER3_BUT_GPIO_CLK RCC_AHB1Periph_GPIOB

//ENCODER4
#define ENCODER4_A_GPIO_PIN GPIO_Pin_2
#define ENCODER4_A_GPIO_PORT GPIOE
#define ENCODER4_A_GPIO_CLK RCC_AHB1Periph_GPIOE

#define ENCODER4_A_EXIT_PORT EXTI_PortSourceGPIOE
#define ENCODER4_A_EXIT_PIN EXTI_PinSource2
#define ENCODER4_A_EXIT_LINE EXTI_Line2

#define ENCODER4_B_GPIO_PIN GPIO_Pin_5
#define ENCODER4_B_GPIO_PORT GPIOE
#define ENCODER4_B_GPIO_CLK RCC_AHB1Periph_GPIOE

#define ENCODER4_BUT_GPIO_PIN GPIO_Pin_6
#define ENCODER4_BUT_GPIO_PORT GPIOE
#define ENCODER4_BUT_GPIO_CLK RCC_AHB1Periph_GPIOE

//ENCODER5
#define ENCODER5_A_GPIO_PIN GPIO_Pin_6
#define ENCODER5_A_GPIO_PORT GPIOA
#define ENCODER5_A_GPIO_CLK RCC_AHB1Periph_GPIOA

#define ENCODER5_A_EXIT_PORT EXTI_PortSourceGPIOA
#define ENCODER5_A_EXIT_PIN EXTI_PinSource6
#define ENCODER5_A_EXIT_LINE EXTI_Line6

#define ENCODER5_B_GPIO_PIN GPIO_Pin_8
#define ENCODER5_B_GPIO_PORT GPIOA
#define ENCODER5_B_GPIO_CLK RCC_AHB1Periph_GPIOA

#define ENCODER5_BUT_GPIO_PIN GPIO_Pin_15
#define ENCODER5_BUT_GPIO_PORT GPIOA
#define ENCODER5_BUT_GPIO_CLK RCC_AHB1Periph_GPIOA

//ENCODER6
#define ENCODER6_A_GPIO_PIN GPIO_Pin_7
#define ENCODER6_A_GPIO_PORT GPIOF
#define ENCODER6_A_GPIO_CLK RCC_AHB1Periph_GPIOF

#define ENCODER6_A_EXIT_PORT EXTI_PortSourceGPIOF
#define ENCODER6_A_EXIT_PIN EXTI_PinSource7
#define ENCODER6_A_EXIT_LINE EXTI_Line7

#define ENCODER6_B_GPIO_PIN GPIO_Pin_8
#define ENCODER6_B_GPIO_PORT GPIOF
#define ENCODER6_B_GPIO_CLK RCC_AHB1Periph_GPIOF

#define ENCODER6_BUT_GPIO_PIN GPIO_Pin_12
#define ENCODER6_BUT_GPIO_PORT GPIOB
#define ENCODER6_BUT_GPIO_CLK RCC_AHB1Periph_GPIOB

//BUTTON CHECK PIN
#define BUTTON_CHECK_GPIO_PIN GPIO_Pin_13
#define BUTTON_CHECK_GPIO_PORT GPIOB
#define BUTTON_CHECK_GPIO_CLK RCC_AHB1Periph_GPIOB
#endif
void KEY_Init(void);
void buttonLooper();

void setButton2Listener(void (*callback)());
void setButton1Listener(void (*callback)());
void setF1ButtonListener(void (*callback)());
void setF2ButtonListener(void (*callback)());
void setF3ButtonListener(void (*callback)());
void setF4ButtonListener(void (*callback)());
void setF5ButtonListener(void (*callback)());

void setEncoder1ButListener(void (*callback)());
void setEncoder2ButListener(void (*callback)());
void setEncoder3ButListener(void (*callback)());
void setEncoder4ButListener(void (*callback)());
void setEncoder5ButListener(void (*callback)());
void setEncoder6ButListener(void (*callback)());

void setEncoder1Listener(void (*callback)(uint8_t));
void setEncoder2Listener(void (*callback)(uint8_t));
void setEncoder3Listener(void (*callback)(uint8_t));
void setEncoder4Listener(void (*callback)(uint8_t));
void setEncoder5Listener(void (*callback)(uint8_t));
void setEncoder6Listener(void (*callback)(uint8_t));

uint8_t isButtonBoardConnected(void);

#endif